package in.iqing.control.api.callback;

import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.List;

import in.iqing.model.bean.Recommend;

/**
 * Created by ZuoShu on 15/7/3.
 */
public abstract class RecommendCallback extends BaseCallback {
    private static final List<Recommend> EMPTY = new ArrayList<>();

    public abstract void onSuccess(List<Recommend> recommends);


    @Override
    public void onSuccess(String data) {
        List<Recommend> emptyDate = new ArrayList<>();
        if (TextUtils.isEmpty(data)) {
            onSuccess(EMPTY);
            return;
        }
        JSONObject jsonObject = JSON.parseObject(data);
        if (jsonObject == null || !jsonObject.containsKey("results")) {
            onSuccess(EMPTY);
            return;
        }
        String resultsString = jsonObject.getString("results");
        if (TextUtils.isEmpty(resultsString)) {
            onSuccess(EMPTY);
            return;
        }
        onSuccess(JSON.parseArray(resultsString, Recommend.class));
    }

}
